package com.bjsxt.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.system.dao.IACateDao;
import com.bjsxt.system.dao.IARegionDao;
import com.bjsxt.system.pojo.ACate;
import com.bjsxt.system.pojo.ARegion;
import com.bjsxt.system.service.ISystemService;

/**
 * 系统Service实现类
 * @author wangshSxt
 *
 */
@Service("systemService")
public class SystemServiceImpl implements ISystemService
{
	@Resource
	private IARegionDao regionDao;
	@Resource
	private IACateDao cateDao;

	@Override
	public ARegion findOneRegionService(Map<String, Object> condMap)
	{
		return this.regionDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneRegionService(ARegion region)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.regionDao.insert(region);
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 二级结构 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", region.getId());
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneRegionService(ARegion region)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.regionDao.update(region);
		
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public List<ARegion> findCondListRegionService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<ARegion> regionList = new ArrayList<ARegion>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			regionList = this.regionDao.findCondList(condMap);
			/* regionList有一条记录,总记录数放到了id属性上 */
			pageInfoUtil.setTotalRecord(regionList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			regionList = this.regionDao.findCondList(condMap);
		}else
		{
			//不分页查询
			regionList = this.regionDao.findCondList(condMap);
		}
		return regionList;
	}
	
	@Override
	public ACate findOneCateService(Map<String, Object> condMap)
	{
		return this.cateDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneCateService(ACate cate)
	{
		JSONObject resultJSON = new JSONObject();
		
		int res = this.cateDao.insert(cate);
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 二级结构 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", cate.getId());
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneCateService(ACate cate)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.cateDao.update(cate);
		
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public List<ACate> findCondListCateService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<ACate> cateList = new ArrayList<ACate>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			cateList = this.cateDao.findCondList(condMap);
			/* cateList有一条记录,总记录数放到了id属性上 */
			pageInfoUtil.setTotalRecord(cateList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			cateList = this.cateDao.findCondList(condMap);
		}else
		{
			//不分页查询
			cateList = this.cateDao.findCondList(condMap);
		}
		return cateList;
	}
}
